import { ApiResponse, User, UserProfile } from '@/types/data'
import { RootThunkAction } from '@/types/store'
import instance from '@/utils/request'

export function getUser(): RootThunkAction {
  return async (dispatch) => {
    const res = await instance.get<ApiResponse<User>>('user')
    // console.log('个人信息', res)
    dispatch({
      type: 'profile/getProfile',
      payload: res.data.data,
    })
  }
}

export function getUserProfile(): RootThunkAction {
  return async (dispatch) => {
    const res = await instance.get<ApiResponse<UserProfile>>('user/profile')
    // console.log('个人资料', res)
    dispatch({
      type: 'profile/getUserProfile',
      payload: res.data.data,
    })
  }
}

export const updataUser = (key: string, value: string): RootThunkAction => {
  return async (dispatch) => {
    await instance.patch('/user/profile', {
      [key]: value,
    })
    dispatch(getUserProfile())
  }
}

export const updataPhoto = (fd: FormData): RootThunkAction => {
  return async (dispatch) => {
    await instance.patch('/user/photo', fd)
    dispatch(getUserProfile())
  }
}
